﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmToKhaiA01TS : Form
    {
        string _selectedID = "";
        string _SoBHXH = "";
        RoleRightInfo listRole;
        DataTable dtSource;
        string _rpType = "";

        public frmToKhaiA01TS(DataTable dtQTBHTrongKy,string _Type)
        {
            InitializeComponent();
            listRole = UIParamater.Quyen_DeNghiCapSoBH;
            BuildVoucherGrid(_Type);
            LoadList(dtQTBHTrongKy,_Type);
            dgvList.ReadOnly = false;
            _rpType = _Type;
        }

        private void GetSelectedID()
        {
            dgvList.EndEdit();
            _selectedID = "";
            foreach (DataGridViewRow row in dgvList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                    _selectedID += row.Cells["Employee_ID"].Value.ToString() + ",";
            }
        }

        private string GetSoSoBHXHChecked()
        {
            dgvList.EndEdit();
            string _SoSoBHXH = "";
            foreach (DataGridViewRow row in dgvList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                    _SoSoBHXH += row.Cells["Detail_InsBookNo"].Value.ToString() + ",";
            }
            return _SoSoBHXH;
        }


        private void BuildVoucherGrid(string _Type)
        {
            dgvList.Columns.Clear();
            //Thêm cột Checkbox
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.Name = "Check";
            col.HeaderText = "Chọn";
            col.Width = 50;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvList.Columns.Add(col);
            col.Visible = true;
            //
            UISetting.AddColumn(ref dgvList, "Detail_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã NV", "", 30, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_DocumentID", "ID Ctừ", "C", 20, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_InsBookNo", "Số sổ BH", "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", "Họ và tên", "L", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_FromDate", "Từ tháng", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_ToDate", "Đến tháng", "C", 90, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_TotalMonth", "Số tháng", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_InsTypeCode", "PA", "R", 40, true, true);
            if (_Type == "ML")
            {
                UISetting.AddColumn(ref dgvList, "Detail_NewAmount", "ML mới", "R", 80, true, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldAmount", "ML cũ", "R", 80, true, true);
            }
            else if (_Type == "HS")
            {
                UISetting.AddColumn(ref dgvList, "Detail_NewBasicFactor", "HSL mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewPositionFactor", "Cvụ mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewOverFactor", "TNVK mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewJobFactor", "TNN mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewAreaFactor", "KV mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldBasicFactor", "HSL cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldPositionFactor", "Cvụ cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldOverFactor", "TNVK cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldJobFactor", "TNN cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldAreaFactor", "KV cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewTotal", "Mức mới", "R", 80, true, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldTotal", "Mức cũ", "R", 80, true, true);
            }

            UISetting.AddColumn(ref dgvList, "BSTang_BHXH", "BS.Tăng BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHXH", "BS.Giảm BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSTang_BHYT", "BS.Tăng BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHYT", "BS.Giảm BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSTang_BHTN", "BS.Tăng BHTN", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHTN", "BS.Giảm BHTN", "R", 60, true, true);

            UISetting.AddColumn(ref dgvList, "Detail_SocialRate", "%BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_MedicalRate", "%BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_UnemployedRate", "%BHTN", "R", 60, true, true);

            UISetting.AddColumn(ref dgvList, "Detail_Type", "HS/ML", "R", 60, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_TakeCard", "Thu thẻ", "R", 60, false, true);

            UISetting.AddColumn(ref dgvList, "Document_BasicSalary", "MLTT", "R", 60, true, true);
        }

        private void LoadList(DataTable dtQTBHTrongKy,string _Type)
        {
            dgvList.Rows.Clear();
            int Counter = 0;
            dtSource = null;
            dtSource = dtQTBHTrongKy.Clone();

            for (int i = 0; i < dtQTBHTrongKy.Rows.Count; i++)
            {
                string PA = dtQTBHTrongKy.Rows[i]["Detail_InsTypeCode"].ToString();
                if ("TC,TD,TM,AD".Contains(PA))
                {
                    DataRow drTmp = dtSource.NewRow();
                    foreach (DataColumn dc in dtSource.Columns)
                        drTmp[dc] = dtQTBHTrongKy.Rows[i][dc.ColumnName];
                    dtSource.Rows.Add(drTmp);
                    
                    //
                    dgvList.Rows.Insert(Counter, 1);
                    dgvList.Rows[Counter].Cells["Detail_ID"].Value = dtQTBHTrongKy.Rows[i]["Detail_ID"].ToString();
                    dgvList.Rows[Counter].Cells["Employee_ID"].Value = dtQTBHTrongKy.Rows[i]["Employee_ID"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_DocumentID"].Value = dtQTBHTrongKy.Rows[i]["Detail_DocumentID"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_InsBookNo"].Value = dtQTBHTrongKy.Rows[i]["Detail_InsBookNo"].ToString();
                    dgvList.Rows[Counter].Cells["Employee_FullName"].Value = dtQTBHTrongKy.Rows[i]["Employee_FullName"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_FromDate"].Value = Convert.ToDateTime(dtQTBHTrongKy.Rows[i]["Detail_FromDate"]).ToString("MM/yyyy");
                    dgvList.Rows[Counter].Cells["Detail_ToDate"].Value = Convert.ToDateTime(dtQTBHTrongKy.Rows[i]["Detail_ToDate"]).ToString("MM/yyyy");
                    dgvList.Rows[Counter].Cells["Detail_InsTypeCode"].Value = dtQTBHTrongKy.Rows[i]["Detail_InsTypeCode"].ToString();

                    if (_Type == "ML")
                    {
                        dgvList.Rows[Counter].Cells["Detail_NewAmount"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAmount"]).ToString("#,##0");
                        dgvList.Rows[Counter].Cells["Detail_OldAmount"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAmount"]).ToString("#,##0");
                    }
                    else if (_Type == "HS")
                    {
                        dgvList.Rows[Counter].Cells["Detail_NewBasicFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewBasicFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewPositionFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewPositionFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewOverFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewOverFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewJobFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewJobFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewAreaFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAreaFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldBasicFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldBasicFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldPositionFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldPositionFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldOverFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldOverFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldJobFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldJobFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldAreaFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAreaFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewTotal"].Value = (Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewBasicFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewPositionFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewOverFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewJobFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAreaFactor"])).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldTotal"].Value = (Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldBasicFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldPositionFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldOverFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldJobFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAreaFactor"])).ToString(UIParamater.Cham3So);
                    }


                    dgvList.Rows[Counter].Cells["BSTang_BHXH"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHXH"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHXH"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHXH"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHXH"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHXH"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSTang_BHYT"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHYT"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHYT"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHYT"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHYT"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHYT"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSTang_BHTN"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHTN"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHTN"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHTN"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHTN"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHTN"]).ToString(UIParamater.Phay2So);

                    dgvList.Rows[Counter].Cells["Detail_SocialRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_SocialRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_SocialRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_MedicalRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_MedicalRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_MedicalRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_UnemployedRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_UnemployedRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_UnemployedRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_Type"].Value = dtQTBHTrongKy.Rows[i]["Detail_Type"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_TakeCard"].Value = dtQTBHTrongKy.Rows[i]["Detail_TakeCard"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_TotalMonth"].Value = Convert.ToInt32(dtQTBHTrongKy.Rows[i]["Detail_TotalMonth"]).ToString();

                    dgvList.Rows[Counter].Cells["Document_BasicSalary"].Value = dtQTBHTrongKy.Rows[i]["Document_BasicSalary"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Document_BasicSalary"]).ToString(UIParamater.Phay2So);
                    
                    Counter++;
                }
            }
            lblTongSo.Text = lblTongSo.Text + Counter.ToString();
        }

        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dgvList.Rows)
            {
                dr.Cells["Check"].Value = true;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dgvList.Rows)
            {
                dr.Cells["Check"].Value = false;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                string SoBHXH = _SoBHXH != "" ? _SoBHXH : "Chờ cấp số";

                ////Gọi nhiều lần để show ra nhiều report
                //foreach (string strID in _selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                //{
                //if (_selectedID.StartsWith(","))
                //    _selectedID = _selectedID.Substring(1);
                //if (_selectedID.EndsWith(","))
                //    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);
                //    DataTable dtRetVal = GetDataTable2Report(dtSource, _selectedID);
                //    frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_Maumoi", _rpType, dtRetVal, new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.MaxValue.ToString(), SoBHXH });
                //    f.Show(this);
                //}

                //Show 1 lần với subreport
                //foreach (string strID in _selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                //{
                if (_selectedID.StartsWith(","))
                    _selectedID = _selectedID.Substring(1);
                if (_selectedID.EndsWith(","))
                    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);
                DataTable dtRetVal = GetDataTable2Report(dtSource, _selectedID);
                //frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_Maumoi", _rpType, dtRetVal, new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.MaxValue.ToString(), SoBHXH });
                frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_TK1_TS", new string[] { _selectedID });
                f.Show(this);
                //}
                //
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private DataTable GetDataTable2Report(DataTable dtSource,string _selectedID)
        {
            DataTable dtRetVal = dtSource.Clone();
            if (_selectedID.StartsWith(","))
                _selectedID = _selectedID.Substring(1);
            if (_selectedID.EndsWith(","))
                _selectedID = _selectedID.Substring(0, _selectedID.LastIndexOf(","));


            string Filter = "Employee_ID IN (" + _selectedID + ")";
            foreach (DataRow MyDataRow in dtSource.Select(Filter))
	            dtRetVal.ImportRow(MyDataRow);
            return dtRetVal;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                if (_selectedID.StartsWith(","))
                    _selectedID = _selectedID.Substring(1);
                if (_selectedID.EndsWith(","))
                    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);

                //foreach(string SoBHXH in _strSoSoBHXH.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries))
                //{
                frmPreviewReport f = new frmPreviewReport("InPhuLucBHXH_NhieuMau", new string[] { _selectedID });
                    f.Show(this);
                //}
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
